package tech.shupi.babytapmobile.view

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import okhttp3.OkHttpClient
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

class RnSecureImageView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ImageView(context, attrs, defStyleAttr) {

    companion object {
        val okHttpClient: OkHttpClient by lazy {
            android.util.Log.d("RnSecureImageView", "初始化 OkHttpClient，准备信任所有证书")
            // 创建一个 TrustManager，用于信任自签名证书
            val trustManagers = arrayOf<TrustManager>(
                object : X509TrustManager {
                    override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {
                        android.util.Log.d("RnSecureImageView", "checkClientTrusted 被调用，authType: $authType, chain: ${chain.size}")
                        // 在这里添加对客户端证书的验证逻辑
                    }

                    override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
                        android.util.Log.d("RnSecureImageView", "checkServerTrusted 被调用，authType: $authType, chain: ${chain.size}")
                        // 在这里添加对自签名证书的验证逻辑，例如检查证书链和证书指纹
                        // 如果验证失败，可以抛出 CertificateException
                    }

                    override fun getAcceptedIssuers(): Array<X509Certificate> {
                        android.util.Log.d("RnSecureImageView", "getAcceptedIssuers 被调用")
                        return arrayOf()
                    }
                }
            )

            // 创建一个 SSLContext
            val sslContext = SSLContext.getInstance("TLS")
            sslContext.init(null, trustManagers, java.security.SecureRandom())
            android.util.Log.d("RnSecureImageView", "SSLContext 初始化完成")

            // 创建 OkHttpClient.Builder 并设置 TLS 协议
            OkHttpClient.Builder()
                .sslSocketFactory(sslContext.socketFactory, trustManagers[0] as X509TrustManager)
                .hostnameVerifier { hostname, session ->
                    android.util.Log.d("RnSecureImageView", "hostnameVerifier 被调用，hostname: $hostname")
                    true
                }
                .build()
        }
    }

    fun setImageUrl(url: String?) {
        android.util.Log.d("RnSecureImageView", "setImageUrl 被调用, url: $url")
        if (url.isNullOrEmpty()) {
            setImageDrawable(null)
            return
        }
        android.util.Log.d("RnSecureImageView", "准备加载图片: $url")
        
        // 使用自定义的 OkHttpClient 初始化 Glide
        Glide.with(this)
            .load(url)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(this)
    }
} 